<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html lang="zh">
<head>
    <title>试卷列表</title>

    <%@include file="/WEB-INF/views/commons/common.jsp" %>

    <link rel="stylesheet" type="text/css" href="${base_href}/static/ui-frame/bootstrap/3.3.5/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css"
          href="${base_href}/static/ui-frame/jquery-easyui/1.4.3/themes/metro/easyui.css">
    <link rel="stylesheet" type="text/css"
          href="${base_href}/static/ui-frame/bootstrap-dialog3/css/bootstrap-dialog.css">
    <link rel="stylesheet" type="text/css"
          href="${base_href}/static/ui-frame/validation-engine/validationEngine.jquery.css">
    <link rel="stylesheet" type="text/css" href="${base_href}/static/css/search-theme.css">
</head>
<body data-grid="easyui">
<div class="path"><span class="right-index">&gt;</span> 现在的位置：试卷列表</div>
<div class="tab-content">
    <div class="fold_ex">
        <h2 class="title-h2">
			<span style="width:100%;">
				<button type="button" class="btn search-btn" data-method="insert"><i class="fa fa-plus"></i>&nbsp;创建试卷&nbsp;</button>
				<button type="button" class="btn search-btn" data-method="pushPaper"><i class="fa fa-upload"></i>&nbsp;全体推送&nbsp;</button>
				<button type="button" class="btn search-btn" data-method="groupPush"><i class="fa fa-upload"></i>&nbsp;班级推送&nbsp;</button>
				<button type="button" class="btn search-btn" data-method="studentPush"><i class="fa fa-upload"></i>&nbsp;学生推送&nbsp;</button>
				<button type="button" class="btn search-btn" data-method="exportPaperUndo"><i
                        class="fa fa-download"></i>&nbsp;导出试卷&nbsp;</button>
				<button type="button" class="btn search-btn" data-method="exportPaperWithAnswer"><i
                        class="fa fa-download"></i>&nbsp;导出试卷带解析&nbsp;</button>
				<button type="button" class="btn search-btn" data-method="exportStudentPapers"><i
                        class="fa fa-download"></i>&nbsp;批量导出学生成绩&nbsp;</button>
				<button type="button" class="btn search-btn" data-method="exportPaper"><i class="fa fa-download"></i>&nbsp;导出试卷统计PPT用&nbsp;</button>
				<button type="button" class="btn search-btn" data-method="exportAnalysisPaper"><i
                        class="fa fa-download"></i>&nbsp;导出测评分析报告&nbsp;</button>
				<button type="button" class="btn search-btn" data-method="changePaperStatus"><i
                        class="fa fa-download"></i>&nbsp;状态变更&nbsp;</button>
                <button type="button" class="btn search-btn" data-method="copyPaper"><i
                                        class="fa fa-download"></i>&nbsp;拷贝试卷&nbsp;</button>
			</span>
            <a href="javascript:void(0);" class="hide-next-btn">收起</a>
        </h2>

        <div>
            <form action="#">
                <table class="search-table container">
                    <tr>
                        <td class="text-right col-xs-2">试卷名称</td>
                        <td class="col-xs-4">
                            <input type="text" class="form-control" name="paperName" value=""
                                   placeholder="支持多关键字的模糊查询"/>
                        </td>
                        <td class="text-right col-xs-2"></td>
                        <td class="col-xs-4">
                        </td>
                    </tr>
                    <tr>
                        <td class="text-right col-xs-2">提问时间起</td>
                        <td class="col-xs-4"><input type="text" class="form-control"
                                                    id="createStartTime" name="createStartTime" value=""
                                                    onFocus="WdatePicker({maxDate:'#F{$dp.$D(\'createEndTime\')}'});"
                                                    onclick="WdatePicker({dateFmt:'yyyy-MM-dd'})" readonly="readonly"/>
                        </td>
                        <td class="text-right col-xs-2">提问时间止</td>
                        <td class="col-xs-4"><input type="text" class="form-control"
                                                    id="createEndTime" name="createEndTime" value=""
                                                    onFocus="WdatePicker({minDate: '#F{$dp.$D(\'createStartTime\')}'});"
                                                    onclick="WdatePicker({dateFmt:'yyyy-MM-dd'})" readonly="readonly"/>
                        </td>
                    </tr>
                    <tr>
                        <td class="text-right col-xs-2">答卷时间起</td>
                        <td class="col-xs-4"><input type="text" class="form-control"
                                                    id="submitStartTime" name="submitStartTime" value=""
                                                    onFocus="WdatePicker({maxDate:'#F{$dp.$D(\'submitEndTime\')}'});"
                                                    onclick="WdatePicker({dateFmt:'yyyy-MM-dd'})" readonly="readonly"/>
                        </td>
                        <td class="text-right col-xs-2">答卷时间止</td>
                        <td class="col-xs-4"><input type="text" class="form-control"
                                                    id="submitEndTime" name="submitEndTime" value=""
                                                    onFocus="WdatePicker({minDate: '#F{$dp.$D(\'submitStartTime\')}'});"
                                                    onclick="WdatePicker({dateFmt:'yyyy-MM-dd'})" readonly="readonly"/>
                        </td>
                    </tr>

                    <tr>
                        <td class="text-right col-xs-2"></td>
                        <td class="text-right">
                            <button type="button" class="btn search-btn" data-method="search"><i
                                    class="fa fa-search"></i>&nbsp;查询&nbsp;
                            </button>
                            <button type="button" class="btn search-btn" data-method="clear"><i class="fa fa-trash"></i>&nbsp;清空&nbsp;
                            </button>
                        </td>
                    </tr>
                </table>
            </form>
        </div>

        <hr class="search-line"/>
        <table class="easyui-datagrid">
            <thead>
            <tr>
                <th data-options="field:'id',checkbox:true">选择</th>
                <th data-options="field:'paperName',width:150,align:'center'">试卷名称</th>
                <th data-options="field:'status',width:50,align:'center'">试卷状态</th>
                <th data-options="field:'createTime',width:50,align:'center'">出卷时间</th>
            </tr>
            </thead>
        </table>
    </div>
</div>

<script type="text/javascript" src="${base_href}/static/ui-frame/jquery/1.11.3/jquery.js"></script>

<script type="text/javascript" src="${base_href}/static/ui-frame/iframe-auto-height/jquery.autoheight.js"></script>
<script type="text/javascript" src="${base_href}/static/ui-frame/bootstrap/3.3.5/js/bootstrap.js"></script>
<script type="text/javascript" src="${base_href}/static/ui-frame/bootstrap-dialog3/js/bootstrap-dialog.js"></script>
<script type="text/javascript" src="${base_href}/static/ui-frame/jquery-easyui/1.4.3/jquery.easyui.min.js"></script>
<script type="text/javascript" src="${base_href}/static/ui-frame/jquery-easyui/1.4.3/easyui.util.js"></script>
<script type="text/javascript"
        src="${base_href}/static/ui-frame/validation-engine/jquery.validationEngine-zh_CN.js"></script>
<script type="text/javascript" src="${base_href}/static/ui-frame/validation-engine/jquery.validationEngine.js"></script>
<script type="text/javascript" src="${base_href}/static/ui-frame/My97DatePicker/WdatePicker.js"></script>
<script type="text/javascript" src="${base_href}/static/ui-frame/layer/layer.js"></script>

<script type="text/javascript" src="${base_href}/static/ui-frame/requirejs/2.2.0/require.js"></script>

<script type="text/javascript">
    requirejs.config({
        baseUrl: '<c:url value="/static/"/>',
        paths: {
            Grid: 'component/grid/js/ms-grid',
        },
        config: {
            'Grid': {contextPath: '<c:url value="/"/>'},
        }
    });

    require(['Grid'], function (Grid) {
        Grid.columns({
            paperName: {
                formatter: function (value, row, index) {
                    var v = value == null ? "" : $('<div/>').text(value).html();
                    return "<a href='javaScript:void(0);' onclick=\"grid.paperName(event, " + index + ")\" class='grid-button' plain='true' title='" + v + "'><div>" + v + "</div></a>";
                },
                event: function (event, index) {
                    var row = this.grid.datagrid('getRows')[index];
                    var param = {paperId: row.id};

                    var url = basePath + "/paper/openAndJumpPaperPage?" + $.param(param);
                    var screenLocation = "width=800px,height=700px,top=0,left=0,menubar=yes,resizable=yes,scrollbars =yes";
                    this.windowOpen(url, "详细信息", screenLocation);
                    event.stopPropagation();
                }
            },
            status: {
                formatter: function (value, row, index) {
                    var vv
                    if (value == 1) {
                        vv = "起草中";
                    } else if (value == 2) {
                        vv = "已发布";
                    } else if (value == 3) {
                        vv = "已失效";
                    } else if (value == 4) {
                        vv = "";
                    }
                    return "<span>" + vv + "</span>"
                }

            }
        });
        Grid.methods({
            insert: function () {
                var url = basePath + "/paper/draftPaper";
                var screenLocation = "width=800px,height=700px,top=0,left=0,menubar=yes,resizable=yes,scrollbars =yes";
                this.windowOpen(url, "详细信息", screenLocation);
            },
            pushPaper: function () {
                var _checked = this.grid.datagrid('getChecked');
                if (_checked.length == 1) {
                    var status = _checked[0].status;
                    if (status == '1') {
                        BootstrapDialog.alert("该试卷尚在起草中，请选择其它试卷。");
                    } else {
                        var that = this;
                        var paperId = _checked[0].id;
                        var url = basePath + "/pushpaper/pushPaper.json";
                        $.post(url, {"paperId": paperId}, function () {
                            that.grid.datagrid('reload', that.queryParam());
                            BootstrapDialog.show({
                                title: "提示框",
                                message: "推送成功",
                                buttons: [{
                                    label: "确认",
                                    action: function (dialog) {
                                        dialog.close();
                                    }
                                }]
                            });

                        }).fail(function (e) {
                            that.grid.datagrid('reload', that.queryParam());
                            BootstrapDialog.show({
                                title: "提示框",
                                message: "推送失败，请稍后再试",
                                buttons: [{
                                    label: "确认",
                                    action: function (dialog) {
                                        dialog.close();
                                    }
                                }]
                            });
                        });
                    }
                } else {
                    BootstrapDialog.alert("请选择一条试卷信息!");
                }
            },
            groupPush: function () {
                var _checked = this.grid.datagrid('getChecked');
                if (_checked.length == 1) {
                    var status = _checked[0].status;
                    if (status == '1') {
                        BootstrapDialog.alert("该试卷尚在起草中，请选择其它试卷。");
                    } else {
                        var that = this;
                        var paperId = _checked[0].id;
                        var url = basePath + "/groupList/groupPaperPage.html?paperId=" + paperId;

                        layer.open({
                            type: 2,
                            title: "依据班级推送试卷",
                            maxmin: true,
                            area: ['1000px', '600px'],
                            content: url
                        });
                    }
                } else {
                    BootstrapDialog.alert("请选择一条试卷信息!");
                }
            },
            studentPush: function () {
                var _checked = this.grid.datagrid('getChecked');
                if (_checked.length == 1) {
                    var status = _checked[0].status;
                    if (status == '1') {
                        BootstrapDialog.alert("该试卷尚在起草中，请选择其它试卷。");
                    } else {
                        var that = this;
                        var paperId = _checked[0].id;
                        var url = basePath + "/studentManager/studentPushPage.html?paperId=" + paperId;

                        layer.open({
                            type: 2,
                            title: "向学生推送试卷",
                            maxmin: true,
                            area: ['1000px', '600px'],
                            content: url
                        });
                    }
                } else {
                    BootstrapDialog.alert("请选择一条试卷信息!");
                }
            },
            exportStudentPapers: function () {
                let _checked = this.grid.datagrid('getChecked');
                if (_checked.length == 1) {
                    let paperId = _checked[0].id;
                    let submitStartTime = $("#submitStartTime").val();
                    let submitEndTime = $("#submitEndTime").val();
                    let url = basePath + "/export/batchDownloadStudentAnswers.json";
                    let param={paperId,submitStartTime,submitEndTime}
                    $.ajax({
                        url: url,
                        type: "post",
                        contentType : 'application/json;charset=UTF-8',
                        dataType: "json",
                        data: JSON.stringify(param),
                        success: function (r) {
                            if(r.code === 0){
                                BootstrapDialog.alert("后台正在生成导出文件，请稍后前往我的下载页下载文件")
                            }else{
                                BootstrapDialog.alert("导出失败！原因：" + r.message)
                            }
                        }
                    })
                } else {
                    BootstrapDialog.alert("请选择一条试卷信息!");
                }
            },
            exportPaperUndo: function () {
                var _checked = this.grid.datagrid('getChecked');
                if (_checked.length == 1) {
                    var paperId = _checked[0].id;
                    BootstrapDialog.show({
                        message: function (content) {    //第二种方式
                            var $message = $("<div></div>");
                            var loadData = content.getData("contentFile");
                            $message.load(loadData);
                            return $message;   //一定记得返回值！
                        },
                        data: {"contentFile": "../static/support/paper/exportPaperParam.html"},
                        buttons: [{
                            label: "取消",
                            cssClass: "btn-primary",   //给按钮添加类名  可以通过此方式给按钮添加样式
                            action: function (dialog) {   //给当前按钮添加点击事件
                                dialog.close();
                            }
                        }, {
                            label: "确认",
                            cssClass: "btn-primary",   //给按钮添加类名  可以通过此方式给按钮添加样式
                            action: function (dialog) {   //给当前按钮添加点击事件
                                var paperHeader = $("[name='paperHeader']").val();
                                var paperTitle = $("[name='paperTitle']").val();
                                if (paperHeader == '' || paperTitle == '') {
                                    alert("请填写参数！");
                                } else {
                                    var url = basePath + "/export/exportWordUndo.json?paperId=" + paperId + "&paperHeader=" + paperHeader + "&paperTitle=" + paperTitle;;
                                    var tempwindow = window.open('_blank'); // 先打开页面
                                    tempwindow.location = url; // 后更改页面地址
                                    dialog.close();
                                }
                            }
                        }
                        ]
                    });


                    window.open(url);
                } else {
                    BootstrapDialog.alert("请选择一条试卷信息!");
                }
            },
            exportPaperWithAnswer: function () {
                var _checked = this.grid.datagrid('getChecked');
                if (_checked.length == 1) {
                    var that = this;
                    var paperId = _checked[0].id;
                    var url = basePath + "/export/exportWordWithAnswer.json?paperId=" + paperId;
                    window.open(url);
                } else {
                    BootstrapDialog.alert("请选择一条试卷信息!");
                }
            }
            ,
            exportPaper: function () {
                var _checked = this.grid.datagrid('getChecked');
                if (_checked.length == 1) {
                    var paperId = _checked[0].id;
                    var url = basePath + "/export/word/exportPaperStatistics.json?paperId=" + paperId;

                    window.open(url);
                } else {
                    BootstrapDialog.alert("请选择一条试卷信息!");
                }
            },
            exportAnalysisPaper: function () {
                var _checked = this.grid.datagrid('getChecked');
                if (_checked.length != 2) {
                    BootstrapDialog.alert("请选择两条条试卷信息!");
                    return;
                }
                var paperName1 = _checked[0].paperName;
                var paperName2 = _checked[1].paperName;
                var paperNameArray1 = paperName1.split("");
                var paperNameArray2 = paperName2.split("");
                if (paperNameArray1.length != paperNameArray2.length) {
                    BootstrapDialog.alert("两张试卷名称不匹配，请重新选择!");
                    return;
                }
                var matchDegree = 0;
                for (var i = 0; i < paperNameArray1.length; i++) {
                    if (paperNameArray1[i] == paperNameArray2[i]) {
                        continue;
                    } else {
                        matchDegree++;
                    }
                }
                if (matchDegree != 1) {
                    BootstrapDialog.alert("两张试卷名称不匹配，请重新选择!");
                    return;
                }
                BootstrapDialog.show({
                    message: function (content) {    //第二种方式
                        var $message = $("<div></div>");
                        var loadData = content.getData("contentFile");
                        $message.load(loadData);
                        return $message;   //一定记得返回值！
                    },
                    data: {"contentFile": "../static/support/content.html"},
                    buttons: [{
                        label: "取消",
                        cssClass: "btn-primary",   //给按钮添加类名  可以通过此方式给按钮添加样式
                        action: function (dialog) {   //给当前按钮添加点击事件
                            dialog.close();
                        }
                    }, {
                        label: "确认",
                        cssClass: "btn-primary",   //给按钮添加类名  可以通过此方式给按钮添加样式
                        action: function (dialog) {   //给当前按钮添加点击事件
                            var countryAvg = $("[name='countryAvg']").val();
                            var reviseScore = $("[name='reviseScore']").val();
                            var subjectName = $("[name='subjectName']").val();
                            var reg = /^\d+(\.\d+)?$/;
                            if (!reg.test(countryAvg) || !reg.test(reviseScore)) {
                                alert("请输入合法数字!");
                                return;
                            }
                            if (countryAvg == '' || reviseScore == '' || subjectName == '') {
                                alert("请填写参数！");
                            } else {
                                let paperId1 = _checked[0].id;
                                let paperId2 = _checked[1].id;
                                let submitStartTime = $("#submitStartTime").val();
                                let submitEndTime = $("#submitEndTime").val();
                                let url = basePath + "/export/analysisPaper/downloadAnalysisPapers.json"
                                let param={paperId1,paperId2,submitStartTime,submitEndTime,countryAvg,reviseScore,subjectName}
                                $.ajax({
                                    url: url,
                                    type: "post",
                                    contentType : 'application/json;charset=UTF-8',
                                    dataType: "json",
                                    data: JSON.stringify(param),
                                    success: function (r) {
                                        if(r.code === 0){
                                            BootstrapDialog.alert("后台正在生成导出文件，请稍后前往我的下载页下载文件")
                                        }else{
                                            BootstrapDialog.alert("导出失败！原因：" + r.message)
                                        }
                                        dialog.close();
                                    }
                                })
                            }


                        }
                    }
                    ]
                });
            },
            changePaperStatus: function () {
                let _checked = this.grid.datagrid('getChecked');
                var that = this;
                if (_checked.length == 1) {
                    let paperId = _checked[0].id;
                    let status = _checked[0].status;
                    console.log(status);
                    if(status == '1'){
                      BootstrapDialog.alert("草稿状态的试卷不支持失效!");
                      return;
                    }
                    let url = basePath + "/paper/changeStatus.json";
                    $.post(url, {"paperId": paperId}, function () {
                        that.grid.datagrid('reload', that.queryParam());
                        BootstrapDialog.show({
                            title: "提示框",
                            message: "变更成功",
                            buttons: [{
                                label: "确认",
                                action: function (dialog) {
                                    dialog.close();
                                }
                            }]
                        });

                    }).fail(function (e) {
                        that.grid.datagrid('reload', that.queryParam());
                        BootstrapDialog.show({
                            title: "提示框",
                            message: "变更失败，请稍后再试",
                            buttons: [{
                                label: "确认",
                                action: function (dialog) {
                                    dialog.close();
                                }
                            }]
                        });
                    });
                } else {
                    BootstrapDialog.alert("请选择一条试卷信息!");
                }
            },
            copyPaper: function () {
                let _checked = this.grid.datagrid('getChecked');
                var that = this;
                if (_checked.length == 1) {
                    let paperId = _checked[0].id;
                    let url = basePath + "/paper/copyPaper.json";
                    $.post(url, {"paperId": paperId}, function () {
                        that.grid.datagrid('reload', that.queryParam());
                        BootstrapDialog.show({
                            title: "提示框",
                            message: "拷贝成功",
                            buttons: [{
                                label: "确认",
                                action: function (dialog) {
                                    dialog.close();
                                }
                            }]
                        });

                    }).fail(function (e) {
                        that.grid.datagrid('reload', that.queryParam());
                        BootstrapDialog.show({
                            title: "提示框",
                            message: "拷贝成功，请稍后再试",
                            buttons: [{
                                label: "确认",
                                action: function (dialog) {
                                    dialog.close();
                                }
                            }]
                        });
                    });
                } else {
                    BootstrapDialog.alert("请选择一条试卷信息!");
                }
            }
        });
        $(function () {
            window.grid = Grid.events();
        })
    })
</script>


</body>
</html>
